package com.fengke.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.fengke.dao.admin.AdminManager;
import com.fengke.dao.model.AdminModel;
import com.fengke.dao.model.AdminRoleModel;
import com.fengke.dao.model.RoleModel;
import com.fengke.service.AgentProfileService;
import com.fengke.service.vo.AgentProfileVO;
import com.fengke.service.vo.RoleVO;
import com.fengke.util.Constants;
import com.fengke.util.LoggerFactory;
import com.fengke.util.MD5Util;
import com.fengke.util.StringUtil;
import com.fengke.util.file.FileUtil;

public class AgentProfileServiceImpl implements AgentProfileService {
	private Logger logger = LoggerFactory.getLogger(AgentProfileServiceImpl.class);
	private AdminManager adminManager;
	public AgentProfileVO researchAgentProfileByLoginInfo(AgentProfileVO agentProfileVO) throws Exception {
		if(agentProfileVO == null || agentProfileVO.getAdminModel() == null || StringUtil.isEmpty(agentProfileVO.getAdminModel().getUsername()) || StringUtil.isEmpty(agentProfileVO.getAdminModel().getPassword())) {
			return null;
		}
		String username = agentProfileVO.getAdminModel().getUsername();
		logger.debug("[" + username + "] login。。。");
		agentProfileVO.getAdminModel().setPassword(MD5Util.getMD5(agentProfileVO.getAdminModel().getPassword()));
		AdminModel adminModel = adminManager.researchAdminByLoginInfo(agentProfileVO.getAdminModel());
		if(adminModel == null) {
			adminModel = new AdminModel();
			adminModel.setUsername(username);
		} else {
			AdminRoleModel adminRoleModel = new AdminRoleModel();
			adminRoleModel.setAdminModel(adminModel);
			List<AdminRoleModel> adminRoleList;
			try {
				adminRoleList = adminManager.researchAdminRoleList(adminRoleModel);
				agentProfileVO.setAdminRoleModelList(adminRoleList);
			} catch (Exception e) {
				logger.error("researchAgentProfileByLoginInfo researchAdminRoleList Exception! adminRoleModel=" + adminRoleModel, e);
			}
		}
		agentProfileVO.setAdminModel(adminModel);
		return agentProfileVO;
	}
	@Override
	public Integer researchAgentProfileVOListSize(AgentProfileVO agentProfileVO) throws Exception {
		if(agentProfileVO.getAdminModel() == null) {
			agentProfileVO.setAdminModel(new AdminModel());
		}
		
		return adminManager.researchAdminListSize(agentProfileVO.getAdminModel());
	}
	@Override
	public List<AgentProfileVO> researchAgentProfileVOList(AgentProfileVO agentProfileVO, int offset, int limit) throws Exception {
		if(agentProfileVO.getAdminModel() == null) {
			agentProfileVO.setAdminModel(new AdminModel());
		}
		List<AdminModel> adminModelList = adminManager.researchAdminModelList(agentProfileVO.getAdminModel(), offset, limit);
		if(adminModelList != null && adminModelList.size() > 0) {
			List<AgentProfileVO> voList = new ArrayList<AgentProfileVO>();
			for(int i = 0; i < adminModelList.size(); i ++) {
				AgentProfileVO vo = new AgentProfileVO();
				vo.setAdminModel(adminModelList.get(i));
				voList.add(vo);
			}
			return voList;
		}
		
		return null;
	}
	@Override
	public boolean checkAdminUsernameUnique(AgentProfileVO agentProfileVO) throws Exception {
		Integer adminNum = adminManager.checkAdminUsernameUnique(agentProfileVO.getAdminModel());
		if(adminNum == null || adminNum <= 0) {
			return true;
		}
		return false;
	}
	@Override
	public Long createAgentProfile(AgentProfileVO agentProfileVO) throws Exception {
		if(agentProfileVO == null || agentProfileVO.getAdminModel() == null) {
			return null;
		}
		
		// 图片处理
		agentProfileVO.getAdminModel().setIdHandsImg(FileUtil.uploadImageFile(agentProfileVO.getIdHandsImg()));
		agentProfileVO.getAdminModel().setIdFrontImg(FileUtil.uploadImageFile(agentProfileVO.getIdFrontImg()));
		agentProfileVO.getAdminModel().setIdBackImg(FileUtil.uploadImageFile(agentProfileVO.getIdBackImg()));
		
		agentProfileVO.getAdminModel().setPassword(MD5Util.getMD5(agentProfileVO.getAdminModel().getPassword()));
		agentProfileVO.getAdminModel().setLoginFailureCount(0);
		agentProfileVO.getAdminModel().setLockedDate(null);
		agentProfileVO.getAdminModel().setLoginDate(null);
		agentProfileVO.getAdminModel().setLoginIp(null);
		agentProfileVO.getAdminModel().setIsCheck(true);
		agentProfileVO.getAdminModel().setCreateDate(new Date());
		agentProfileVO.getAdminModel().setModifyDate(new Date());
		agentProfileVO.getAdminModel().setHasEmailAction(false);
		if(agentProfileVO.getAdminModel().getIsLocked() == null) {
			agentProfileVO.getAdminModel().setIsLocked(false);
		}
		
		Long profileId = adminManager.createAdminModel(agentProfileVO.getAdminModel());
		if(profileId != null) {
			agentProfileVO.getAdminModel().setId(profileId);
			// 权限处理
			String[] roles = agentProfileVO.getAgentRoles().split(",");
			if(roles != null && roles.length > 0) {
				for(int i = 0; i < roles.length; i ++) {
					if(!StringUtil.isEmpty(roles[i])) {
						AdminRoleModel arModel = new AdminRoleModel();
						arModel.setAdminModel(agentProfileVO.getAdminModel());
						RoleModel roleModel = new RoleModel();
						roleModel.setId(Long.parseLong(roles[i].trim()));
						arModel.setRoleModel(roleModel);
						adminManager.createAdminRoleModel(arModel);
					}
				}
			}
		}
		
		return profileId;
	}
	@Override
	public List<RoleVO> researchRoleVOList(RoleVO roleVO) throws Exception {
		if(roleVO == null) {
			roleVO = new RoleVO();
		}
		if(roleVO.getRoleModel() == null) {
			roleVO.setRoleModel(new RoleModel());
		}
		List<RoleModel> roleModelList = adminManager.researchRoleList(roleVO.getRoleModel());
		if(roleModelList != null && roleModelList.size() > 0) {
			List<RoleVO> voList = new ArrayList<RoleVO>();
			for(int i = 0; i < roleModelList.size(); i ++) {
				RoleVO vo = new RoleVO();
				vo.setRoleModel(roleModelList.get(i));
				voList.add(vo);
			}
			return voList;
		}
		return null;
	}
	@Override
	public AgentProfileVO researchAgentProfileById(AgentProfileVO agentProfileVO) throws Exception {
		if(agentProfileVO == null || agentProfileVO.getAdminModel() == null || agentProfileVO.getAdminModel().getId() == null) {
			return null;
		}
		AdminModel adminModel = adminManager.researchAdminById(agentProfileVO.getAdminModel());
		if(adminModel == null) {
			return null;
		} else {
			AdminRoleModel adminRoleModel = new AdminRoleModel();
			adminRoleModel.setAdminModel(adminModel);
			List<AdminRoleModel> adminRoleList;
			try {
				adminRoleList = adminManager.researchAdminRoleList(adminRoleModel);
				agentProfileVO.setAdminRoleModelList(adminRoleList);
			} catch (Exception e) {
				logger.error("researchAgentProfileById researchAdminRoleList Exception! adminRoleModel=" + adminRoleModel, e);
			}
		}
		agentProfileVO.setAdminModel(adminModel);
		return agentProfileVO;
	}
	@Override
	public int updateAgentProfileById(AgentProfileVO agentProfileVO) throws Exception {
		if(agentProfileVO == null || agentProfileVO.getAdminModel() == null || agentProfileVO.getAdminModel().getId() == null) {
			return 0;
		}
		// 删除权限
		List<String> idList = new ArrayList<String>();
		idList.add(String.valueOf(agentProfileVO.getAdminModel().getId()));
		adminManager.deleteAdminRoleByAdminIds(idList);
		// 新增
		String[] roles = agentProfileVO.getAgentRoles().split(",");
		if(roles != null && roles.length > 0) {
			for(int i = 0; i < roles.length; i ++) {
				if(!StringUtil.isEmpty(roles[i])) {
					AdminRoleModel arModel = new AdminRoleModel();
					arModel.setAdminModel(agentProfileVO.getAdminModel());
					RoleModel roleModel = new RoleModel();
					roleModel.setId(Long.parseLong(roles[i].trim()));
					arModel.setRoleModel(roleModel);
					adminManager.createAdminRoleModel(arModel);
				}
			}
		}
		
		// 图片处理
		if(agentProfileVO.getIdHandsImg() != null && !StringUtil.isEmpty(agentProfileVO.getIdHandsImg().getFileName())) {
			agentProfileVO.getAdminModel().setIdHandsImg(FileUtil.uploadImageFile(agentProfileVO.getIdHandsImg()));
		}
		if(agentProfileVO.getIdFrontImg() != null && !StringUtil.isEmpty(agentProfileVO.getIdFrontImg().getFileName())) {
			agentProfileVO.getAdminModel().setIdFrontImg(FileUtil.uploadImageFile(agentProfileVO.getIdFrontImg()));
		}
		if(agentProfileVO.getIdBackImg() != null && !StringUtil.isEmpty(agentProfileVO.getIdBackImg().getFileName())) {
			agentProfileVO.getAdminModel().setIdBackImg(FileUtil.uploadImageFile(agentProfileVO.getIdBackImg()));
		}
		
		agentProfileVO.getAdminModel().setPassword(MD5Util.getMD5(agentProfileVO.getAdminModel().getPassword()));
		agentProfileVO.getAdminModel().setLoginDate(null);
		agentProfileVO.getAdminModel().setLoginIp(null);
		agentProfileVO.getAdminModel().setIsCheck(true);
		agentProfileVO.getAdminModel().setModifyDate(new Date());
		agentProfileVO.getAdminModel().setHasEmailAction(false);
		
		if(agentProfileVO.getAdminModel().getIsLocked() != null && agentProfileVO.getAdminModel().getIsLocked()) {
			agentProfileVO.getAdminModel().setIsLocked(true);
			agentProfileVO.getAdminModel().setLoginFailureCount(Constants.CSC_LOGIN_ERROR_TIMES);
			agentProfileVO.getAdminModel().setLockedDate(new Date());
		} else {
			AdminModel hisAdminModel = adminManager.researchAdminById(agentProfileVO.getAdminModel());
			if(hisAdminModel != null && hisAdminModel.getIsLocked() && !agentProfileVO.getAdminModel().getIsLocked()) {
				agentProfileVO.getAdminModel().setIsLocked(false);
				agentProfileVO.getAdminModel().setLoginFailureCount(0);
				agentProfileVO.getAdminModel().setLockedDate(null);
			} else {
				agentProfileVO.getAdminModel().setIsLocked(hisAdminModel.getIsLocked());
				agentProfileVO.getAdminModel().setLoginFailureCount(hisAdminModel.getLoginFailureCount());
				agentProfileVO.getAdminModel().setLockedDate(hisAdminModel.getLockedDate());
			}
		}
		
		return adminManager.updateAdminById(agentProfileVO.getAdminModel());
	}
	@Override
	public int deleteAgentProfileByIds(List<String> list) throws Exception {
		if(list == null || list.size() <= 0) {
			return 0;
		}
		// 级联删除权限
		adminManager.deleteAdminRoleByAdminIds(list);
		// 删除商家
		return adminManager.deleteAdminByIds(list);
	}
	@Override
	public AgentProfileVO researchAgentProfileByUsername(AgentProfileVO agentProfileVO) throws Exception {
		if(agentProfileVO == null || agentProfileVO.getAdminModel() == null || StringUtil.isEmpty(agentProfileVO.getAdminModel().getUsername())) {
			return null;
		}
		AdminModel adminModel = new AdminModel();
		adminModel.setUsername(agentProfileVO.getAdminModel().getUsername());
		List<AdminModel> adminModelList = adminManager.researchAdminModelList(adminModel, 0, Integer.MAX_VALUE);
		if(adminModelList != null && adminModelList.size() > 0) {
			agentProfileVO.setAdminModel(adminModelList.get(0));
			return agentProfileVO;
		}
		return null;
	}
	@Override
	public int updateAgentProfileLoginSuccessInfo(AgentProfileVO agentProfileVO) throws Exception {
		if(agentProfileVO == null || agentProfileVO.getAdminModel() == null || agentProfileVO.getAdminModel().getId() == null) {
			return 0;
		}
		return adminManager.updateAdminLoginSuccessInfo(agentProfileVO.getAdminModel());
	}
	@Override
	public int updateAgentProfileLoginFailureInfo(AgentProfileVO agentProfileVO) throws Exception {
		if(agentProfileVO == null || agentProfileVO.getAdminModel() == null || agentProfileVO.getAdminModel().getId() == null) {
			return 0;
		}
		return adminManager.updateAdminLoginFailureInfo(agentProfileVO.getAdminModel());
	}
	public AdminManager getAdminManager() {
		return adminManager;
	}
	public void setAdminManager(AdminManager adminManager) {
		this.adminManager = adminManager;
	}
}
